package ee.az.test.geometry.block;

import ee.az.geometry.Angle;

/**
 * @author Artur Zinatullin (artur.zinatullin@gmail.com) 2010
 */
public class AngleArc extends ee.az.geometry.AngleArc {

	public AngleArc(double lower, double higher) {
		super(lower, higher);
	}
	
	// XXX there is a better way
	public AngleArc(ee.az.geometry.AngleArc a) {
		super(a.getLower(), a.getHigher());
	}

	@Override
	public AngleArc sum(ee.az.geometry.AngleArc arc) {
		// XXX there is a better way
		return new AngleArc(super.sum(arc));
	}
	
	public static AngleArc getArc(int x, int y, int centerX, int centerY) {
		double[] angles = new double [] {
				Angle.normalize(Angle.getAngle(x, y, centerX+0.5, centerY+0.5)),
				Angle.normalize(Angle.getAngle(x+1, y, centerX+0.5, centerY+0.5)),
				Angle.normalize(Angle.getAngle(x, y+1, centerX+0.5, centerY+0.5)),
				Angle.normalize(Angle.getAngle(x+1, y+1, centerX+0.5, centerY+0.5))
		};
		AngleArc result = getArc(angles);
		if (result.getHigher() - result.getLower() > Math.PI) { // angle around 0
			for(int i=0; i<angles.length; i++) {
				if (angles[i] > Math.PI) {
					angles[i] -= 2*Math.PI;
				}
			}
			result = getArc(angles);
		}
		return result;
	}
	
	public static AngleArc getArc(double[] angles) {
		double low = Double.POSITIVE_INFINITY;
		double high = Double.NEGATIVE_INFINITY;
		for (double a : angles) {
			low = Math.min(low, a);
			high = Math.max(high, a);
		}
		return new AngleArc(low, high);
	}
}
